//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ExchangeRateManager.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/ExchangeRate.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (boost::shared_ptr<QuantLib::ExchangeRateManager>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRateManager = NULL;
#endif
	_ppExchangeRateManager = &childNative;
    
}
Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (QuantLib::ExchangeRateManager& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phExchangeRateManager = NULL;
#endif
	_ppExchangeRateManager = new boost::shared_ptr<QuantLib::ExchangeRateManager> (&childNative);
    
    _ExchangeRateManagerOwner = owner;
    
}

Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (CExchangeRateManager^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRateManager = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExchangeRateManager = new boost::shared_ptr<QuantLib::ExchangeRateManager> (copy->GetShared());
        
    }
}
Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRateManager = NULL;
#endif
	if (!t->IsSubclassOf(CExchangeRateManager::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (QuantLib::Handle<QuantLib::ExchangeRateManager>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExchangeRateManager = &childNative;
	_ppExchangeRateManager = &static_cast<boost::shared_ptr<QuantLib::ExchangeRateManager>>(childNative.currentLink());
    
    _ExchangeRateManagerOwner = owner;
}
Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (QuantLib::Handle<QuantLib::ExchangeRateManager> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExchangeRateManager = &childNative;
	_ppExchangeRateManager = &static_cast<boost::shared_ptr<QuantLib::ExchangeRateManager>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CExchangeRateManager::CExchangeRateManager (QuantLib::ExchangeRateManager childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeRateManager = NULL;
#endif
	_ppExchangeRateManager = new boost::shared_ptr<QuantLib::ExchangeRateManager> (new QuantLib::ExchangeRateManager (childNative));
    
}
#endif

Cephei::QL::Currencies::CExchangeRateManager::~CExchangeRateManager ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppExchangeRateManager != NULL)
    {
	    delete _ppExchangeRateManager;
        _ppExchangeRateManager = NULL;
    }
}
Cephei::QL::Currencies::CExchangeRateManager::!CExchangeRateManager ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppExchangeRateManager != NULL)
    {
	    delete _ppExchangeRateManager;
    }
}
QuantLib::ExchangeRateManager& Cephei::QL::Currencies::CExchangeRateManager::GetReference ()
{
    if (_ppExchangeRateManager == NULL) throw gcnew NativeNullException ();
	return **_ppExchangeRateManager;
}
boost::shared_ptr<QuantLib::ExchangeRateManager>& Cephei::QL::Currencies::CExchangeRateManager::GetShared ()
{
    if (_ppExchangeRateManager == NULL) throw gcnew NativeNullException ();
	return *_ppExchangeRateManager;
}
QuantLib::ExchangeRateManager* Cephei::QL::Currencies::CExchangeRateManager::GetPointer ()
{
    if (_ppExchangeRateManager == NULL) throw gcnew NativeNullException ();
	return &**_ppExchangeRateManager;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExchangeRateManager>& Cephei::QL::Currencies::CExchangeRateManager::GetHandle ()
{
	if (_phExchangeRateManager == NULL)
	{
		_phExchangeRateManager = new Handle<QuantLib::ExchangeRateManager> (*_ppExchangeRateManager);
	}
	return *_phExchangeRateManager;
}
#endif
bool Cephei::QL::Currencies::CExchangeRateManager::HasNative () 
{
	return (_ppExchangeRateManager != NULL);
}

Cephei::QL::Currencies::IExchangeRateManager^ Cephei::QL::Currencies::CExchangeRateManager::Add (Cephei::QL::IExchangeRate^ rate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ endDate)
{
    CExchangeRate^ _Crate;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Crate = safe_cast<CExchangeRate^> (rate);
        _Crate->Lock();
        QuantLib::ExchangeRate& _rate = static_cast<QuantLib::ExchangeRate&> (_Crate->GetReference ()); 
        QuantLib::Date _startDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (startDate) ? (QuantLib::Date)ValueHelper::Convert (startDate->Value) : Date::minDate()); //9a
        QuantLib::Date _endDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (endDate) ? (QuantLib::Date)ValueHelper::Convert (endDate->Value) : Date::maxDate()); //9a
    	(*_ppExchangeRateManager)->add ( _rate,  _startDate,  _endDate );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
    }
}
Cephei::QL::Currencies::IExchangeRateManager^ Cephei::QL::Currencies::CExchangeRateManager::Clear::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppExchangeRateManager)->clear ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IExchangeRate^ Cephei::QL::Currencies::CExchangeRateManager::Lookup (Cephei::QL::ICurrency^ source, Cephei::QL::ICurrency^ target, Microsoft::FSharp::Core::FSharpOption<DateTime>^ date, Microsoft::FSharp::Core::FSharpOption<QL::ExchangeRate::TypeEnum>^ type)
{
    CCurrency^ _Csource;
    CCurrency^ _Ctarget;
    try
    {
        _Csource = safe_cast<CCurrency^> (source);
        _Csource->Lock();
        QuantLib::Currency& _source = static_cast<QuantLib::Currency&> (_Csource->GetReference ()); 
        _Ctarget = safe_cast<CCurrency^> (target);
        _Ctarget->Lock();
        QuantLib::Currency& _target = static_cast<QuantLib::Currency&> (_Ctarget->GetReference ()); 
        QuantLib::Date _date = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (date) ? (QuantLib::Date)ValueHelper::Convert (date->Value) : QuantLib::Date()); //9a
        QuantLib::ExchangeRate::Type _type = 
            (Microsoft::FSharp::Core::FSharpOption<QL::ExchangeRate::TypeEnum>::IsSome::get (type) ? (QuantLib::ExchangeRate::Type)type->Value : QuantLib::ExchangeRate::Derived ); //5
    	QuantLib::ExchangeRate _rv = (QuantLib::ExchangeRate)(*_ppExchangeRateManager)->lookup ( _source,  _target,  _date,  _type );   
        Cephei::QL::CExchangeRate^ _nrv = gcnew Cephei::QL::CExchangeRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Csource != nullptr) _Csource->Unlock();
        if (_Ctarget != nullptr) _Ctarget->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

